package com.irate.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.widget.Toast;

import com.irate.activity.R;
import com.irate.http.AsyncHttpResponseHandler;
import com.irate.http.RequestParams;
import com.irate.model.UserSearchFriend;
import com.irate.provider.Constant;
import com.irate.service.AsyncHTTPRest;

public class UserSearchFriendDAO {

	public List<UserSearchFriend> parseListData(String json) {
		List<UserSearchFriend> list = new ArrayList<UserSearchFriend>();
		try {

			JSONObject obj = new JSONObject(json);

			try {

				JSONArray array = obj.getJSONArray("message");
				for (int i = 0; i < array.length(); i++) {
					JSONObject fobj = array.getJSONObject(i);
					extractData(list, fobj);
				}
			} catch (Exception e) {
				JSONObject object = obj.getJSONObject("message");
				extractData(list, object);
			}

		} catch (Exception e) {

		}
		return list;
	}

	private void extractData(List<UserSearchFriend> list, JSONObject fobj)
			throws JSONException {
		UserSearchFriend f = new UserSearchFriend();

		f.setEmail(fobj.getString("user_name"));
		f.setId(fobj.getString("user_id"));
		f.setIsFriend(fobj.getString("is_friend"));
		f.setSend_request(fobj.getString("sent_request"));
		f.setIs_requested(fobj.getString("is_requested"));
		try {
			f.setUser_photo(fobj.getString("user_photo"));
		} catch (Exception e) {

		}

		list.add(f);
	}

	public void sendRequestFriend(String user_id,
			AsyncHttpResponseHandler handler, Context context,
			ProgressDialog dialog) {
		ConnectivityManager con = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (con == null || con.getActiveNetworkInfo() == null
				|| !con.getActiveNetworkInfo().isConnected()) {

			if (dialog != null)
				dialog.dismiss();

			Toast.makeText(
					context,
					context.getResources().getString(R.string.connection_error),
					Toast.LENGTH_LONG).show();
		} else {
			String link = Constant.URL + "/" + Constant.SERVICE_CATEGORY_FRIEND
					+ "/" + Constant.SERVICE_REQUEST_FRIEND;

			RequestParams param = new RequestParams();
			param.put("user_id", user_id);
			Header[] headers = { new BasicHeader("X-API-KEY", Constant.XAPIKEY) };

			AsyncHTTPRest.post(link, headers, param, handler);
		}
	}

	public void acceptRequestFriend(String request_id,
			AsyncHttpResponseHandler handler, Context context,
			ProgressDialog dialog) {
		ConnectivityManager con = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (con == null || con.getActiveNetworkInfo() == null
				|| !con.getActiveNetworkInfo().isConnected()) {

			if (dialog != null)
				dialog.dismiss();

			Toast.makeText(
					context,
					context.getResources().getString(R.string.connection_error),
					Toast.LENGTH_LONG).show();
		} else {
			String link = Constant.URL + "/" + Constant.SERVICE_CATEGORY_FRIEND
					+ "/" + Constant.SERVICE_ACCEPT_FRIEND;

			RequestParams param = new RequestParams();
			param.put("request_id", request_id);
			Header[] headers = { new BasicHeader("X-API-KEY", Constant.XAPIKEY) };

			AsyncHTTPRest.post(link, headers, param, handler);
		}
	}

	public void denyRequestFriend(String request_id,
			AsyncHttpResponseHandler handler, Context context,
			ProgressDialog dialog) {
		ConnectivityManager con = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (con == null || con.getActiveNetworkInfo() == null
				|| !con.getActiveNetworkInfo().isConnected()) {

			if (dialog != null)
				dialog.dismiss();

			Toast.makeText(
					context,
					context.getResources().getString(R.string.connection_error),
					Toast.LENGTH_LONG).show();
		} else {
			String link = Constant.URL + "/" + Constant.SERVICE_CATEGORY_FRIEND
					+ "/" + Constant.SERVICE_DENY_FRIEND;

			RequestParams param = new RequestParams();
			param.put("request_id", request_id);
			Header[] headers = { new BasicHeader("X-API-KEY", Constant.XAPIKEY) };

			AsyncHTTPRest.post(link, headers, param, handler);
		}
	}
}
